package com.yl.nettylearning.netty.day12;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ReplayingDecoder;

import java.util.List;

/**
 * @author yangl
 */
public class MessageDecoder extends ReplayingDecoder<Void> {
    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
        System.out.println("MessageDecoder 的 decode 方法被调用");
        //需要将得到的二进制字节码转成 MessageProtocol数据包
        int len = in.readInt();
        byte[] content = new byte[len];
        in.readBytes(content);
        //封装成MessageProtocol对象，放入out,传递给下一个handler处理
        MessageProtocol messageProtocol = new MessageProtocol();
        messageProtocol.setLen(len);
        messageProtocol.setContent(content);
        out.add(messageProtocol);

    }
}
